package com.main;

import com.qrcode.SQRCode;


import com.source.*;
import org.junit.Test;


import java.io.File;
import java.sql.Timestamp;
import java.util.*;

import com.emailManager.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class JTest extends TestBase{

    @Test
    public void testEs() {
        SElastic sElastic = SElastic.getSingleton();
        SElasticBase base1 = new SElasticBase();
        base1.index = "nametest";
        base1.type = "type1";
        base1.id = "1";
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name","我老爸");
        base1.setJsonMap(map);
//
//        sElastic.creatIndexInPinyin(base1);
//        sElastic.addDocument(base1);
//        Map<String, Object> map = sElastic.getDocument(base);


//        System.out.print(map);
        SElasticBase base = new SElasticBase();
        base.index = "nametest";
        base.type = "type1";
//        base.index = "newtest";
//        base.type = "ytpe1";
        /******** base ********/
        base.pageIndex = 0;
        base.pageSize = 10;
//        /******** query ********/
//        List<SElasticSingle> singles = new ArrayList<SElasticSingle>();
//        SElasticSingle single1 = new SElasticSingle();
//        single1.value = "liu";
//        single1.type = SESEnum.must;
//        singles.add(single1);
//        SElasticSingle single2 = new SElasticSingle();
//        single2.value = "文字";
//        single2.type = SESEnum.should;
//        singles.add(single2);
//        base.singleList = singles;
//        /******** term ********/
//        List<SElasticTerm> termList = new ArrayList<SElasticTerm>();
//        for (int i = 0;i<1;i++){
//            SElasticTerm term = new SElasticTerm();
//            term.key = "name";
//            term.value = "wo";
//            term.type = SESEnum.must;
//            term.isPhrase = true;
//            termList.add(term);
//        }
//        base.terms = termList;
//        /******** range ********/
//        List<SElasticRange> rangeList = new ArrayList<SElasticRange>();
//        for (int k = 0;k<1;k++){
//            SElasticRange range1 = new SElasticRange();
//            range1.key = "age";
//            range1.from = String.valueOf(20);
//            range1.to = String.valueOf(30);
//            range1.type = SESEnum.should;
//            rangeList.add(range1);
//            SElasticRange range2 = new SElasticRange();
//            range2.key = "age";
//            range2.from = String.valueOf(50);
//            range2.to = String.valueOf(60);
//            range2.type = SESEnum.should;
//            rangeList.add(range2);
//        }
//        base.ranges = rangeList;
//        /******** field ********/
//        List<String> fieldList = new ArrayList<String>();
//        fieldList.add("count");
//        base.fields = fieldList;
//        /******** sort ********/
//        List<SElasticSort> sortList = new ArrayList<SElasticSort>();
//        SElasticSort sort = new SElasticSort();
//        sort.isASC_DESC =false;
//        sort.key = "age";
//        sortList.add(sort);
//        base.sorts = sortList;

        Object a = sElastic.getDocuments_ik(base);
        System.out.print(a);
    }




    @Test
    public void testEs2() {
        Date date = new Date();
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        Date date2 = new Date(date.getTime() - gc.get(gc.HOUR_OF_DAY) * 60 * 60
                * 1000 - gc.get(gc.MINUTE) * 60 * 1000 - gc.get(gc.SECOND)
                * 1000);
        Timestamp timestamp = new Timestamp(date2.getTime());
        System.out.println(date2.getTime()/1000);
        System.out.println(timestamp);
//        SElastic sElastic = SElastic.getSingleton();
//        sElastic.deleteIndex("index_ali");

//        sElastic.creatIndexInPinyin("pingyintest1");
//        sElastic.addPinyinMapping("pingyintest1","types","name");

//
//        SElasticBase base1 = new SElasticBase();
//        base1.index = "index_ali";
//        base1.type = "type2";
//        base1.id = "3";
//        Map<String,Object> map = new HashMap<String, Object>();
////        map.put("poi_info","1024");
//        map.put("poi_title","1024");
////        map.put("poi_title","德罗伊葡萄酒，精品天秤，法国玛谢络徳酒庄");
//        map.put("poi_info","德罗伊葡萄酒，精品天秤，法国玛谢络徳酒庄11111");
//        base1.setJsonMap(map);
//        sElastic.addDocument(base1);

//        sElastic.addAliasForIndex("pois","index_ali");
//        boolean s111 = sElastic.isExists("index");
//

//        SElasticBase base = new SElasticBase();
//        base.index = "index_ali";
//        base.type = "type2";
//        List<SElasticTerm> termList = new ArrayList<SElasticTerm>();
//        for (int i = 0;i<1;i++){
//            SElasticTerm term = new SElasticTerm();
//            term.key = "poi_title";
//            term.value = "玛谢络徳";
//            term.type = SESEnum.must;
//            term.isPhrase = false;
//            termList.add(term);
//        }
//        base.terms = termList;
//        List<SElasticSingle> list = new ArrayList<SElasticSingle>();
//        SElasticSingle single = new SElasticSingle();
//        single.type = SESEnum.must;
//        single.value = "玛谢络徳";
//        base.singleList = list;
//        Object a = sElastic.getDocuments_ik(base);
//        System.out.print(a);

//        sElastic.creatIndexInPinyin("pois","type1");
//        sElastic.addPinyinMapping("pois","type2","");
    }






    public void recomeList(List<Map<String,String>> list,List<List<Map<String,String>>> gotList,List<String> names){
        if (list.size()<1){
            return;
        }
        List<Map<String,String>> listPick = new ArrayList<Map<String, String>>();
        List<Map<String,String>> listUnPick = new ArrayList<Map<String, String>>();
        String mallCode = list.get(0).get("key");
        names.add(mallCode);
        for (Map<String,String> comd : list){
            if (comd.get("key").equals(mallCode)){
                listPick.add(comd);
            }
            else {
                listUnPick.add(comd);
            }
        }
        gotList.add(listPick);
        recomeList(listUnPick,gotList,names);
    }


    @Test
    public void testGetGood() {

//        String string = SAES.enAes("mallCode=SCCompany0CCMall0&goodCode=ISD1508684519");
//        String base = SAES.unAes(string);
//        System.out.println(base);
//        String string = encrypt("mallCode=SCCompany0CCMall0&goodCode=ISD1508684519");
//        String base = decrypt(string);
//        System.out.println(base);
//        byte[] bytes = "mallCode=SCCompany0CCMall0&goodCode=ISD1508684519".getBytes();
//        try {
//            String string1 = new BASE64Encoder().encode(encrypt(bytes,CRYPT_KEY));
//            String string2 = new BASE64Encoder().encode(decrypt(string1.getBytes(),CRYPT_KEY));
//            System.out.print(string1+"~~~~"+string2);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }



//        List<Map<String,String>> list = new ArrayList<Map<String, String>>();
//        for (int i =0 ;i<10;i++){
//            long random = (long)((Math.random()*3+1));
//            Map<String,String> map = new HashMap<String, String>();
//            map.put("key",String.valueOf(random));
//            list.add(map);
//        }
//        List<List<Map<String,String>>> gotList = new ArrayList<List<Map<String, String>>>();
//        List<String> names = new ArrayList<String>();
//        recomeList(list,gotList,names);
//        System.out.print(gotList);
//        System.out.print(names);


//
//        long random = (long)((Math.random()*9+1)*1000*10000*10000);
//
//
//
//
//        String bb = "393719469@qq.com";
//        String wxUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6c17988f80d49ade" +
//            "&redirect_uri=https://mall.isoops.com/loginConfrimView.html"+
//            "&response_type=code&scope=snsapi_userinfo&state=mc:SCCompany0CCMall0,login:0," +
//            "rc:123456,cc:,lt:,lc:#wechat_redirect";
//
//
//        SQRCode sqrCode = new SQRCode();
//        File file = sqrCode.creatQrcodeImage(wxUrl,"123455");
//        semail(bb,"thread",file);
//        file.delete();
//



        // 需要加密的字串
        String cSrc = "mallCode=SCCompany0CCMall0&goodCode=ISD1508684519";
        System.out.println("加密前的字串是："+cSrc);
        // 加密
        String enString = null;
        try {
            enString = encrypt1(cSrc,"utf-8","LSSAmuelLSSAmuel","LSSAmuelLSSAmuel");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("加密后的字串是："+ enString);
        // 解密
        String DeString = null;
        try {
            DeString = decrypt1(enString,"utf-8","LSSAmuelLSSAmuel","LSSAmuelLSSAmuel");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("解密后的字串是：" + DeString);
    }





    public void semail(String addr,String contect,File file){
        SEmail sEmail = new SEmail();
        boolean bol = sEmail.sendMailForResetPassword(addr,"测试",contect,file);
    }







    // 加密
    public String encrypt1(String sSrc, String encodingFormat, String sKey, String ivParameter) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());//使用CBC模式，需要一个向量iv，可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes(encodingFormat));
        return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码。
    }

    // 解密
    public String decrypt1(String sSrc, String encodingFormat, String sKey, String ivParameter) throws Exception {
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original,encodingFormat);
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }








    private static final String AES = "AES/CBC/PKCS5Padding";

    private static final String CRYPT_KEY = "LSSAmuelLSSAmuel";

    /**
     * 加密
     *
     * @return
     */
    public static byte[] encrypt(byte[] src, String key) throws Exception {
        Cipher cipher = Cipher.getInstance(AES);
        SecretKeySpec securekey = new SecretKeySpec(key.getBytes(), AES);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes());//使用CBC模式，需要一个向量iv，可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, securekey,iv);//设置密钥和加密形式
        return cipher.doFinal(src);
    }

    /**
     * 解密
     *
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] src, String key)  throws Exception  {
        Cipher cipher = Cipher.getInstance(AES);
        SecretKeySpec securekey = new SecretKeySpec(key.getBytes(), AES);//设置加密Key
        IvParameterSpec iv = new IvParameterSpec(key.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, securekey,iv);//设置密钥和解密形式
        return cipher.doFinal(src);
    }

    /**
     * 二行制转十六进制字符串
     *
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
        }
        return hs.toUpperCase();
    }

    public static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException("长度不是偶数");
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }

    /**
     * 解密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public final static String decrypt(String data) {
        try {
            return new String(decrypt(hex2byte(data.getBytes()),
                    CRYPT_KEY));
        } catch (Exception e) {
        }
        return null;
    }
    /**
     * 加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public final static String encrypt(String data) {
        try {
            return byte2hex(encrypt(data.getBytes(), CRYPT_KEY));
        } catch (Exception e) {
        }
        return null;
    }



}
